package com.me.blood.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.me.ad.ADType;
import com.me.bean.Xt;
import com.me.blood.R;
import com.me.blood.databinding.AcInputBinding;
import com.me.blood.frag.BaseDialogFragment;
import com.me.blood.frag.DelTipDlg;
import com.me.blood.frag.TimePickerDialogFragment;
import com.me.db.DBListener;
import com.me.db.XtDao;
import com.me.utils.ToastUtil;
import com.me.utils.Utils;
import com.ycuwq.datepicker.date.DatePickerDialogFragment;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class InputActivity extends BaseActivity {

    AcInputBinding binding;
    Calendar calendar;
    int type = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = AcInputBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.back.setOnClickListener(v -> finish());

        calendar = Calendar.getInstance();

        Xt item = null;
        if (getIntent().hasExtra("data")) {
            item = getIntent().getParcelableExtra("data");
        }

        if (item != null) {
            binding.title.setText("血糖历史记录");
            binding.seek.setEnabled(false);
            binding.seek.setProgress((int) ((20 * item.value) - 10) / 3);
            binding.dateTv.setText(Utils.getDay(item.time));
            binding.timeTv.setText(Utils.getHour(item.time));
            binding.btn.setText("删除");
            binding.btn.setTextColor(0xff3a3a3a);
            binding.btn.setBackgroundResource(R.drawable.gray_btn_bg);
            binding.btn.setOnClickListener(view -> {
                DelTipDlg dlg = new DelTipDlg();
                dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                    @Override
                    public void onDismiss(boolean isOk, Object value) {
                        if (isOk) {
                            setResult(RESULT_OK);
                            finish();
                        }
                    }
                });
                dlg.show(getSupportFragmentManager(), "del");
            });

            int[][] icons = new int[][]{{R.mipmap.sj, R.mipmap.sj_select}, {R.mipmap.wfq, R.mipmap.wfq_select}, {R.mipmap.wfh, R.mipmap.wfh_check}, {R.mipmap.wfq1, R.mipmap.wfq1_select}};
            List<TextView> list = new ArrayList<>();
            list.add(binding.tv4);
            list.add(binding.tv1);
            list.add(binding.tv2);
            list.add(binding.tv3);
            changeType(item.type, list, icons);

        } else {
            binding.seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    float p = progress / 100f;
                    float now = 14 * p + 1;
                    DecimalFormat decimalFormat = new DecimalFormat("#0.0"); // 设置格式化模式为保留两位小数
                    String result = decimalFormat.format(now);
                    binding.value.setText(result);
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });
            binding.date.setOnClickListener(v -> pickDate());
            binding.time.setOnClickListener(v -> pickTime());
            binding.btn.setOnClickListener(v -> add());

            int[][] icons = new int[][]{{R.mipmap.sj, R.mipmap.sj_select}, {R.mipmap.wfq, R.mipmap.wfq_select}, {R.mipmap.wfh, R.mipmap.wfh_check}, {R.mipmap.wfq1, R.mipmap.wfq1_select}};
            List<TextView> list = new ArrayList<>();
            list.add(binding.tv4);
            list.add(binding.tv1);
            list.add(binding.tv2);
            list.add(binding.tv3);
            for (int i = 0; i < list.size(); i++) {
                int j = i;
                list.get(i).setOnClickListener(view -> changeType(j, list, icons));
            }
        }
    }

    private void changeType(int i, List<TextView> list, int[][] icons) {
        type = i;
        for (int j = 0; j < list.size(); j++) {
            if (i == j) {
                list.get(j).setTextColor(0xff3a3a3a);
                list.get(j).setCompoundDrawablesRelativeWithIntrinsicBounds(0, icons[j][1], 0, 0);
            } else {
                list.get(j).setTextColor(0xffA8AAAB);
                list.get(j).setCompoundDrawablesRelativeWithIntrinsicBounds(0, icons[j][0], 0, 0);
            }
        }
    }

    private void pickDate() {
        DatePickerDialogFragment datePickerDialogFragment = new DatePickerDialogFragment();
        datePickerDialogFragment.setOnDateChooseListener(new DatePickerDialogFragment.OnDateChooseListener() {
            @Override
            public void onDateChoose(int i, int i1, int i2) {
                StringBuffer sb = new StringBuffer();
                sb.append(i).append("/");
                if (i1 < 10) {
                    sb.append(0);
                }
                sb.append(i1).append("/");
                if (i2 < 10) {
                    sb.append(0);
                }
                sb.append(i2);
                calendar.set(Calendar.YEAR, i);
                calendar.set(Calendar.MONTH, i1 - 1);
                calendar.set(Calendar.DAY_OF_MONTH, i2);
                binding.dateTv.setText(sb.toString());
            }
        });
        datePickerDialogFragment.show(getSupportFragmentManager(), "date");
    }

    private void pickTime() {
        TimePickerDialogFragment datePickerDialogFragment = new TimePickerDialogFragment();
        datePickerDialogFragment.setOnDateChooseListener(new TimePickerDialogFragment.OnTimeChooseListener() {
            @Override
            public void onTimeChoose(int hour, int min) {
                StringBuffer sb = new StringBuffer();
                if (hour < 10) {
                    sb.append(0);
                }
                sb.append(hour).append(":");
                if (min < 10) {
                    sb.append(0);
                }
                sb.append(min);
                calendar.set(Calendar.HOUR_OF_DAY, hour);
                calendar.set(Calendar.MINUTE, min);
                binding.timeTv.setText(sb.toString());
            }
        });
        datePickerDialogFragment.show(getSupportFragmentManager(), "date");
    }

    private void add() {
        String date = binding.dateTv.getText().toString();
        if (TextUtils.isEmpty(date)) {
            ToastUtil.showShortToast("请选择日期");
            return;
        }
        getReward(ADType.SAVE_REWARD, new Runnable() {
            @Override
            public void run() {
                Xt bean = new Xt();
                bean.time = calendar.getTimeInMillis();
                bean.value = Float.valueOf(binding.value.getText().toString().trim());
                bean.type = type;
                new XtDao().insert(bean, new DBListener() {
                    @Override
                    public void onResult(boolean bo, String msg, Object result) {
                        if (bo) {
                            h.post(() -> {
                                ToastUtil.showShortToast("添加成功");
                                setResult(RESULT_OK);
                                finish();
                            });
                        } else {
                            h.post(() -> {
                                ToastUtil.showShortToast(TextUtils.isEmpty(msg) ? "添加失败" : msg);
                            });
                        }
                    }
                });
            }
        });
    }

    @Override
    protected ADType getXpType() {
        return ADType.ADD_XP;
    }
}
